package pl.edu.pjwstk.kor.database;

import java.util.ArrayList;
import java.util.List;

import pl.edu.pjwstk.kor.database.init.AddressInitalizer;
import pl.edu.pjwstk.kor.database.init.CategoriesInitalizer;
import pl.edu.pjwstk.kor.database.init.CustomerInitalizer;
import pl.edu.pjwstk.kor.database.init.DealInitalizer;
import pl.edu.pjwstk.kor.database.init.EmployeeInitalizer;
import pl.edu.pjwstk.kor.database.init.RankInitalizer;
import pl.edu.pjwstk.kor.database.init.ServiceInitalizer;
import pl.edu.pjwstk.kor.model.Address;
import pl.edu.pjwstk.kor.model.Category;
import pl.edu.pjwstk.kor.model.Customer;
import pl.edu.pjwstk.kor.model.Deal;
import pl.edu.pjwstk.kor.model.Employee;
import pl.edu.pjwstk.kor.model.Rank;
import pl.edu.pjwstk.kor.model.Service;

import com.db4o.ObjectContainer;

public class DataInit {
	
	public static void main(String[] args) throws Exception {
		Db4oDatabase db = new Db4oDatabase(true);
		
		List<Object> objectsToStore = new ArrayList<Object>();
		
		List<Address> addresses = new AddressInitalizer().initData();
		List<Employee> employees = new EmployeeInitalizer().initData();
		List<Customer> customers = new CustomerInitalizer(addresses).initData();
		List<Category> categories = new CategoriesInitalizer().initData();
		List<Rank> ranks = new RankInitalizer(customers, categories).initData();
		List<Service> services = new ServiceInitalizer(categories).initData();
		List<Deal> deals = new DealInitalizer(customers, employees, services).initData();
		
		objectsToStore.addAll(addresses);
		objectsToStore.addAll(employees);
		objectsToStore.addAll(customers);
		objectsToStore.addAll(categories);
		objectsToStore.addAll(services);
		objectsToStore.addAll(deals);
		objectsToStore.addAll(ranks);
		
		System.out.println("Read data done");
		
		for(Object o : objectsToStore) {
			db.getObjectContainer().store(o);
//			System.out.println("stored: " + o);
		}
		
		db.getObjectContainer().commit();
		System.out.println("Data saved [" + objectsToStore.size() + "]");
		
		int readCount = 0;
		readCount += readObject(Address.class, db.getObjectContainer());
		readCount += readObject(Employee.class, db.getObjectContainer());
		readCount += readObject(Customer.class, db.getObjectContainer());
		readCount += readObject(Category.class, db.getObjectContainer());
		readCount += readObject(Service.class, db.getObjectContainer());
		readCount += readObject(Deal.class, db.getObjectContainer());
		readCount += readObject(Rank.class, db.getObjectContainer());
		
		if(readCount == objectsToStore.size()) {
			System.out.println("Data is valid " + readCount + " out of " + objectsToStore.size() + " stored");
		} else {
			System.err.println("Data is invalid. Stored: " + objectsToStore.size() + " read: " + readCount);
		}
		
	}
	
	@SuppressWarnings("unused")
	private static int readObject(Class<?> clazz, ObjectContainer objContainer) {
		int readCount = 0;
		for(Object o : objContainer.query(clazz)) {
//			System.out.println("read: " + e);
			readCount++;
		}
		return readCount;
	}
}
